﻿// -----------------------------------------------------------------------
// <copyright file="GeoLocation.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace WPForms.Common.Model
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Serialization;

    /// <summary>
    /// FormField for locations
    /// </summary>
    [Description("Represents a location on Earth.")]
    public class GeoLocation : FormField
    {
        /// <summary>
        /// Gets or sets the altitute.
        /// </summary>
        public double Altitute { get; set; }

        /// <summary>
        /// Gets or sets the latitude.
        /// </summary>
        public double Latitude { get; set; }

        /// <summary>
        /// Gets or sets the longitude.
        /// </summary>
        public double Longitude { get; set; }

        /// <summary>
        /// Gets or sets the friendly, resolved location.
        /// </summary>
        public string FriendlyLocation { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether location is overriden by the <see cref="FriendlyLocation"/> property.
        /// </summary>
        public bool IsLocationOverriden { get; set; }

        /// <summary>
        /// Writes the field specific data into the specified builder.
        /// </summary>
        /// <param name="builder">The builder.</param>
        public override void Write(StringBuilder builder)
        {
            using (var writer = new System.IO.StringWriter())
            {
                var serializer = new XmlSerializer(typeof(GeoLocation));
                serializer.Serialize(writer, this);
                builder.Append(writer.ToString());
            }
        }

        /// <summary>
        /// Intializez the field from the given stored value.
        /// </summary>
        /// <param name="storedValue">The stored value.</param>
        public override void Read(string storedValue)
        {
            var serializer = new XmlSerializer(typeof(GeoLocation));
            using (var reader = new System.IO.StringReader(storedValue))
            {
                var location = (GeoLocation)serializer.Deserialize(reader);
                this.Altitute = location.Altitute;
                this.FriendlyLocation = location.FriendlyLocation;
                this.Latitude = location.Latitude;
                this.Longitude = location.Longitude;
                this.IsLocationOverriden = location.IsLocationOverriden;
            }
        }

        /// <summary>
        /// Validates the element.
        /// </summary>
        /// <returns>
        /// The result of the validation.
        /// </returns>
        public override IEnumerable<string> Validate()
        {
            if (this.IsRequired &&
               this.Altitute == 0 && this.Longitude == 0 && this.Latitude == 0 && string.IsNullOrEmpty(this.FriendlyLocation))
            {
                yield return this.Label + " is required";
            }
        }
    }
}
